<script setup lang="ts">
import { getPaymentRecordByUserIdApi } from '@src/apis/paymentProjectApi.ts'
import { onMounted, onUnmounted, ref } from 'vue'
import { ElTable } from 'element-plus'
import { getImageUrlByImageName } from '@src/apis/imageApi.ts'
import Pagination from '@src/components/Pagination.vue'
import formatDate from '@util/formatDate.ts'
import ImagePreview from '@src/components/ImagePreview.vue'
interface User {
  id: number
  name: string
  classId: number
}
interface PaymentProject {
  id: number
  title: string
  description: string
  amount: number
  classId: number
}
interface PaymentRecord {
  id: number
  time: Date
  beforeClassBalance: number
  userId: number
  classId: number
  paymentProjectId: number
  imageName: string
  user: User
  paymentProject: PaymentProject
}
// 储存全部缴费记录变量
const tableData = ref<PaymentRecord[]>([])
// 展示某条缴费记录信息变量
const itemForm = ref({
  id: 0,
  time: new Date(),
  beforeClassBalance: 0,
  userId: 0,
  classId: 0,
  paymentProjectId: 0,
  imageName: '1',
  user: {
    id: 0,
    name: '1',
    classId: 0,
  },
  paymentProject: {
    id: 0,
    title: '1',
    description: '1',
    amount: 0,
    classId: 0,
  },
})
// 显示展示某条信息表单框变量
const dialogFormVisible1 = ref(false)
// 显示缴费图片
const dialogImageUrl = ref('')
// 当前显示的是第几页
const currentPage = ref(1)
// 每页显示多少条
const pageSize = ref(2)
// 总记录条数
const total = ref(0)
// 班级ID
const classId = ref(0)
// 钩子函数调用查询函数
onMounted(() => {
  getPaymentRecordByUserId(1, 2)
})

const getPaymentRecordByUserId = (page: number, size: number) => {
  getPaymentRecordByUserIdApi(page, size).then((res) => {
    tableData.value = res.data.data.records
    total.value = res.data.data.total
  })
}

const handleInfo = (row: PaymentRecord) => {
  dialogFormVisible1.value = true
  itemForm.value = row
  dialogImageUrl.value = getImageUrlByImageName(row.imageName)
}
const handleDialogClose = () => {
  dialogFormVisible1.value = false
}

const handleSizeChange = (size: number) => {
  pageSize.value = size
  getPaymentRecordByUserId(currentPage.value, pageSize.value)
}
const handleCurrentChange = (page: number) => {
  currentPage.value = page
  getPaymentRecordByUserId(currentPage.value, pageSize.value)
}

/**预览图片*/
const imagePreviewProps = ref({
  show: false,
  src: '',
  alt: '',
})
onMounted(() => {
  const previewListener = () => {
    const listener = (e: MouseEvent) => {
      const target = e.target as HTMLElement
      if (target.tagName.toLowerCase() !== 'img') return

      const imgEl = target as HTMLImageElement
      imagePreviewProps.value.src = imgEl.src
      imagePreviewProps.value.alt = imgEl.alt
      imagePreviewProps.value.show = true
    }
    window.addEventListener('click', listener)

    return () => {
      window.removeEventListener('click', listener)
    }
  }
  const removePreviewListener = previewListener()
  onUnmounted(() => {
    removePreviewListener()
  })
})
</script>

<template>
  <div class="overAll">
    <h2><el-text>个人缴费记录</el-text></h2>
    <!--  展示个人缴费记录-->
    <el-table
      height="650"
      ref="singleTableRef"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="80" />
      <el-table-column property="time" label="缴费时间">
        <template #default="scope">
          <div>{{ formatDate(scope.row.time) }}</div>
        </template>
      </el-table-column>
      <el-table-column
        class="table-column"
        property="paymentProject.title"
        label="项目标题"
      />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button type="primary" @click="handleInfo(scope.row)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 查看缴费记录信息-->
    <div class="paymentRecordInfo">
      <el-dialog
        width="400px"
        v-model="dialogFormVisible1"
        title="查看缴费记录"
        :before-close="handleDialogClose"
        center
      >
        <el-form label-width="120px">
          <el-form-item label="缴费时间">
            <div class="block">
              <el-date-picker
                v-model="itemForm.time"
                type="datetime"
                placeholder="请点击选择截止时间"
                format="YYYY-MM-DD HH:mm:ss"
                date-format="MMM DD, YYYY"
                time-format="HH:mm"
                disabled
              />
            </div>
          </el-form-item>
          <el-form-item label="缴费项目名">
            <el-input
              v-model="itemForm.paymentProject.title"
              style="width: 210px"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="缴费项目描述">
            <el-input
              type="textarea"
              v-model="itemForm.paymentProject.description"
              style="width: 210px"
              disabled
              class="textArea"
              rows="5"
              resize="none"
              readonly
            ></el-input>
          </el-form-item>
          <el-form-item label="缴费金额(元)">
            <el-input
              v-model="itemForm.paymentProject.amount"
              style="width: 210px"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="缴费截图图片">
            <img
              :src="dialogImageUrl"
              style="width: 100px; height: 100px; margin-left: 25px"
              alt=""
            />
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <Pagination
      :page="currentPage"
      :size="pageSize"
      @size-change="handleSizeChange"
      @page-change="handleCurrentChange"
      :total="total"
    >
    </Pagination>
    <image-preview
      v-model:show="imagePreviewProps.show"
      v-model:src="imagePreviewProps.src"
      v-model:alt="imagePreviewProps.alt"
    />
  </div>
</template>

<style scoped lang="less">
.overAll {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  width: 96%;
  height: 98%;
  h2 {
    text-align: center;
    .el-text {
      font-size: 2em;
    }
  }
}
.page {
  position: fixed;
  bottom: 5px;
  left: 210px;
}
</style>
